package com.whl.sm.service.impl.inhabitant;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.inhabitant.AssetsOwnerMapper;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.inhabitant.AssetsOwner;
import com.whl.sm.model.inhabitant.AssetsTenant;
import com.whl.sm.model.system.Role;
import com.whl.sm.model.system.UserCpRole;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.inhabitant.AssetsOwnerService;
import com.whl.sm.service.inhabitant.AssetsTenantService;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.service.system.RoleService;
import com.whl.sm.service.system.UserCpRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-14
 */
@Service
public class AssetsOwnerServiceImpl extends ServiceImpl<AssetsOwnerMapper, AssetsOwner> implements AssetsOwnerService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserCpRoleService userCpRoleService;

    @Autowired
    private AssetsTenantService assetsTenantService;

    @Autowired
    private OrgUserService orgUserService;

    @Lazy
    @Autowired
    private ComUnitsService comUnitsService;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public Result saveBind(AssetsOwner assetsOwner) {
        assetsOwner.setOperator(orgUserService.getCurrentUserId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("assets_id",assetsOwner.getAssetsId());
        queryWrapper.ne("is_del",1);

        List<AssetsTenant> hasTenList = assetsTenantService.list(queryWrapper);
        if (hasTenList.size()>0){
            return Result.build(444,"该单元已绑定租户！");
        }
        if (assetsOwner.getOwnerType() == 1){
            queryWrapper.eq("owner_type",1);
        }
        else {
            queryWrapper.eq("owner_type",0);
        }
        QueryWrapper limit = (QueryWrapper) queryWrapper.clone();
        queryWrapper.eq("user_id",assetsOwner.getUserId());
        List<AssetsOwner> hasList = getBaseMapper().selectList(queryWrapper);
        if (hasList.size()>0){
            return Result.build(444,"该用户已绑定单元！");
        }
        hasList = getBaseMapper().selectList(limit);
        if (assetsOwner.getOwnerType() == 1 && hasList.size()>0){
            return Result.build(444,"该单位已绑定业主！");
        }
        if (assetsOwner.getOwnerType() == 0 && hasList.size()>7){
            return Result.build(444,"该单元绑定成员已达上限!");
        }
        if (assetsOwner.getOwnerType() ==1){
            ComUnits linkUnit = comUnitsService.getById(assetsOwner.getAssetsId());
            linkUnit.setUnitStatus(1);
            comUnitsService.update(linkUnit);
        }
        getBaseMapper().insert(assetsOwner);
        Role owner = roleService.getOne(new QueryWrapper<Role>().eq("role_name","小区业主"));
        Role liver = roleService.getOne(new QueryWrapper<Role>().eq("role_name","小区住户"));
        UserCpRole userCpRole = userCpRoleService.getOne(new QueryWrapper<UserCpRole>().eq("user_id",assetsOwner.getUserId()));
        if (userCpRole != null && assetsOwner.getOwnerType() ==1 && !(owner.getId()).equals(userCpRole.getRoleId())){
            userCpRole.setRoleId(owner.getId());
            userCpRoleService.save(userCpRole);
        }
        else if (userCpRole == null){
            UserCpRole insert = new UserCpRole();
            insert.setUserId(assetsOwner.getUserId());
            if (assetsOwner.getOwnerType() == 1){
                insert.setRoleId(owner.getId());
            }else {
                insert.setRoleId(liver.getId());
            }
            userCpRoleService.save(insert);

        }
        return Result.ok();
    }
}
